package com.syy.hive;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class VideoMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    private Text text = new Text();
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        StringBuilder result = new StringBuilder();
        String s = value.toString();
        String[] split = s.split("\t");
        if (split.length >= 9) {
            result.append(split[0].replace(" ", "")).append("\t");
            try {
                result.append(split[1].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[2].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[3].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[4].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[5].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[6].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[7].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                result.append(split[8].replace(" ", "")).append("\t");
            } catch (Exception e) {
                e.printStackTrace();
            }

            StringBuilder relatedIds = new StringBuilder();
            for (int i = 9; i < split.length; i++) {
                relatedIds.append(split[i]);
                relatedIds.append("&");
            }

            result.append(relatedIds);

            text.set(result.toString());

            context.write(text, NullWritable.get());
        }

    }
}
